//package com.ycy.token;
//
//import com.aliyuncs.CommonRequest;
//import com.aliyuncs.CommonResponse;
//import com.aliyuncs.DefaultAcsClient;
//import com.aliyuncs.IAcsClient;
//import com.aliyuncs.exceptions.ClientException;
//import com.aliyuncs.http.MethodType;
//import com.aliyuncs.profile.DefaultProfile;
//import com.sun.org.apache.xml.internal.security.utils.Base64;
//import io.netty.handler.codec.http.HttpMethod;
//import org.junit.Test;
//
//import javax.crypto.Mac;
//import javax.crypto.SecretKey;
//import javax.crypto.spec.SecretKeySpec;
//import java.io.UnsupportedEncodingException;
//import java.net.URLEncoder;
//
//public class AlibabaYouXiang {
//
//  public static void main(String[] args) {
//    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
//    IAcsClient client = new DefaultAcsClient(profile);
//
//    CommonRequest request = new CommonRequest();
//    request.setSysMethod(MethodType.POST);
//    request.setSysDomain("dysmsapi.aliyuncs.com");
//    request.setSysVersion("2017-05-25");
//    request.setSysAction("SendSms");
//    request.putQueryParameter("RegionId", "cn-hangzhou");
//
//    try {
//      CommonResponse response = client.getCommonResponse(request);
//      System.out.println(response.getData());
//    } catch (ClientException e) {
//      e.printStackTrace();
//    }
//  }
//
//  private static final String MAC_NAME = "HmacSHA1";
//  private static final String ENCODING = "UTF-8";
//
//  /**
//   * 使用 HMAC-SHA1 签名方法对对encryptText进行签名
//   *
//   * @param encryptText 被签名的字符串
//   * @param encryptKey  密钥
//   * @return
//   * @throws Exception
//   */
//  public static byte[] HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
//    byte[] data = encryptKey.getBytes(ENCODING);
////根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
//    SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
////生成一个指定 Mac 算法的 Mac 对象
//    Mac mac = Mac.getInstance(MAC_NAME);
////用给定密钥初始化 Mac 对象
//    mac.init(secretKey);
//    byte[] text = encryptText.getBytes(ENCODING);
////完成 Mac 操作
//    return mac.doFinal(text);
//  }
//
//  @Test
//  public void test() throws Exception {
///*StringToSign=
//                HTTPMethod + “&” +
//                percentEncode(“/”) + ”&” +
//percentEncode(CanonicalizedQueryString*/
//    String str = "AccessKeyId=testid&AccountName=<a%b'>&Action=SingleSendMail&AddressType=1&Format=XML&HtmlBody=4&RegionId=cn-hangzhou&ReplyToAddress=true&SignatureMethod=HMAC" +
//            "-SHA1&SignatureNonce=c1b2c332-4cfb-4a0f-b8cc-ebe622aa0a5c&SignatureVersion=1.0&Subject=3&TagName=2&Timestamp=2016-10-20T06:27:56Z&ToAddress=1@test.com&Version=2015-11-23";
//    String percentStr = "";
//    String[] strs = str.split("&");
//    for (int i = 0; i < strs.length; i++) {
//      String[] str1 = strs[i].split("=");
//      if (str1.length == 1) {
//        percentStr = percentStr + getUtf8Encoder(str1[0]) + "=" + getUtf8Encoder("") + "&";
//      } else {
//        percentStr = percentStr + getUtf8Encoder(str1[0]) + "=" + getUtf8Encoder(str1[1]) + "&";
//      }
//    }
//    percentStr = percentStr.substring(0, percentStr.lastIndexOf("&"));
//    String percent = URLEncoder.encode("/", "UTF-8");
//    percentStr = getUtf8Encoder(percentStr);
//    String toSign = HttpMethod.POST + "&" + percent + "&" + percentStr;
//    System.out.println("--------------" + toSign);
//    /*POST&%2F&AccessKeyId%3Dtestid&AccountName%3D%253Ca%2525b%2527%253E&Action%3DSingleSendMail&AddressType%3D1&Format%3DXML&HtmlBody%3D4&RegionId%3Dcn-hangzhou&ReplyToAddress%3Dtrue&SignatureMethod%3DHMAC-SHA1&SignatureNonce%3Dc1b2c332-4cfb-4a0f-b8cc-ebe622aa0a5c&SignatureVersion%3D1.0&Subject%3D3&TagName%3D2&Timestamp%3D2016-10-20T06%253A27%253A56Z&ToAddress%3D1%2540test.com&Version%3D2015-11-23*/
//    byte[] bytes = HmacSHA1Encrypt(toSign, "testsecret&");
//    String base64Str = Base64.encode(bytes);
//    System.out.println(base64Str);//llJfXJjBW3OacrVgxxsITgYaYm0=
//  }
//
//  private String getUtf8Encoder(String param) throws UnsupportedEncodingException {
//    return URLEncoder.encode(param, "UTF-8")
//            .replace("+", "%20")
//            .replace("*", "%2A")
//            .replace("%7E", "~");
//  }
//
//  ;
//
//}
